package com.geek.factory.controller;


import com.geek.factory.constant.MessageConstant;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.Menu;
import com.geek.factory.result.Result;
import com.geek.factory.service.MenuService;
import com.geek.factory.utils.JwtHelper;
import com.geek.factory.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-06-23
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("get-async-routes")
    public Result getRoutes(@RequestHeader("Authorization") String authorization){
        if(StringUtils.hasLength(authorization)){
            String token = authorization.substring(7);
            Long userId = JwtHelper.getUserId(token);
            List<MenuVO> list = menuService.findAll(userId);
            if(!CollectionUtils.isEmpty(list)){
                cleanEmptyChildren(list); // 递归去除空children
                return new Result(SuccessConstant.SUCCESS , MessageConstant.GET_MENU_SUCCESS , list);
            }
        }
        return new Result(SuccessConstant.FAIL , MessageConstant.GET_MENU_FAIL);
    }

    private void cleanEmptyChildren(List<MenuVO> menuList) {
        if (menuList == null) return;
        for (MenuVO menu : menuList) {
            List<MenuVO> children = menu.getChildren();
            if (children == null || children.isEmpty()) {
                menu.setChildren(null); // 这样序列化时不会输出children字段
            } else {
                cleanEmptyChildren(children); // 递归处理
            }
        }
    }

    @GetMapping
    public Result list() {
        List<Menu> menus = menuService.getAllMenus();
        return new Result(SuccessConstant.SUCCESS, "查询成功", menus);
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        Menu menu = menuService.getMenuById(id);
        return new Result(SuccessConstant.SUCCESS, "查询成功", menu);
    }

    @PostMapping
    public Result add(@RequestBody Menu menu) {
        boolean success = menuService.addMenu(menu);
        if (success) {
            return new Result(SuccessConstant.SUCCESS, "新增成功", true);
        } else {
            return new Result(SuccessConstant.FAIL, "新增失败", false);
        }
    }

    @PutMapping
    public Result update(@RequestBody Menu menu) {
        boolean success = menuService.updateMenu(menu);
        if (success) {
            return new Result(SuccessConstant.SUCCESS, "修改成功", true);
        } else {
            return new Result(SuccessConstant.FAIL, "修改失败", false);
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean success = menuService.deleteMenu(id);
        if (success) {
            return new Result(SuccessConstant.SUCCESS, "删除成功", true);
        } else {
            return new Result(SuccessConstant.FAIL, "删除失败", false);
        }
    }

    /**
     * 级联删除菜单（包括关联的meta数据）
     */
    @DeleteMapping("/{id}/cascade")
    public Result deleteCascade(@PathVariable Integer id) {
        boolean success = menuService.deleteMenuCascade(id);
        if (success) {
            return new Result(SuccessConstant.SUCCESS, "删除成功", true);
        } else {
            return new Result(SuccessConstant.FAIL, "删除失败", false);
        }
    }
}
